package com.xingkeduo.remote;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.dto.ResponseMessage;
import com.xingkeduo.dto.StockSmallProgramVo;
import com.xingkeduo.remote.feign.WordPadFeignService;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class WordPadRemoteService {

    @Resource
    private WordPadFeignService wordPadFeignService;



    public  Map<String,Object> getStockIdByStaffIdAndStoreId(Long staffId,Long storeId,String startTime,String selectDate){
        ResponseMessage responseMessage = wordPadFeignService.getStockIdByStaffIdAndStoreId(staffId,storeId,startTime,selectDate);
        if(responseMessage.getStatus()!=1){
            throw new BusinessException(BusinessException.Type.DEFAULT, "预约时间不存在");
        }
        Object data = responseMessage.getData();
        Map<String,Object> map= (Map<String, Object>) data;
        return map;
    }


    public  Map<String,Object> getStockIdByStaffIdAndBaberStoreId(Long staffId,Long storeId,String startTime,String selectDate){
        log.info("getStockIdByStaffIdAndBaberStoreId staffId:{}", JSON.toJSONString(staffId));
        log.info("getStockIdByStaffIdAndBaberStoreId storeId:{}", JSON.toJSONString(storeId));
        log.info("getStockIdByStaffIdAndBaberStoreId startTime:{}", JSON.toJSONString(startTime));
        log.info("getStockIdByStaffIdAndBaberStoreId selectDate:{}", JSON.toJSONString(selectDate));
        ResponseUtils<List<StockSmallProgramVo>> responseMessage = wordPadFeignService.getStockIdByStaffIdAndBaberStoreId(staffId,storeId,startTime,selectDate);
        log.info("双库存 getStockIdByStaffIdAndBaberStoreId:{}", JSON.toJSONString(responseMessage));
        if(responseMessage.getStatus()!=1){
            throw new BusinessException(BusinessException.Type.DEFAULT, "预约时间不存在");
        }
        Map<String,Object> map= new HashMap<>();
        try {
            List<StockSmallProgramVo> data = responseMessage.getData();
            map.put("id",data.get(0).getId()+","+data.get(1).getId());
            map.put("startTime",  data.get(0).getStartTime());
            map.put("endTime",  data.get(1).getEndTime());
        }catch (Exception e){
            throw new BusinessException(BusinessException.Type.DEFAULT, "预约时间不存在");
        }
        return map;
    }


    /**
     *
     * @param dateTime
     * @param staffId
     * @param storeId
     * @return
     */
    public  Object getLineNum(String dateTime,String staffId, String storeId){
        log.info("getLineNum dateTime:{}", JSON.toJSONString(dateTime));
        log.info("getLineNum staffId:{}", JSON.toJSONString(staffId));
        log.info("getLineNum storeId:{}", JSON.toJSONString(storeId));
        ResponseMessage responseMessage = wordPadFeignService.getLineNum(dateTime,staffId,storeId);
        log.info("getLineNum responseMessage:{}", JSON.toJSONString(responseMessage));
        if(responseMessage.getStatus()!=1){
            throw new BusinessException(BusinessException.Type.DEFAULT, "获取排队号失败");
        }
        Object data = responseMessage.getData();
        Map<String,Object> map= (Map<String, Object>) data;
        boolean bf =(Boolean)map.get("lineUp");
        if(bf){
            return map.get("lineNo");
        }
        return bf;
    }



}
